<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2020/7/31
 * Time: 22:17
 */
class serv_rep_dxm extends L {

    /**
     * 描述 : 获取订单列表
     * 作者 : Zoro.Zhu
     */
    public static function getOrderList($cookie, $pageNo = 1) {
        $params = [
            'pageNo' => $pageNo,
            'pageSize' => "300",
            'shopId' => "-1",
            'state' => "",
            'platform' => "",
            'isSearch' => "1",
            'searchType' => "orderId",
            'authId' => "-1",
            'startTime' => "",
            'endTime' => "",
            'country' => "",
            'orderField' => "order_create_time",
            'isVoided' => "-1",
            'isRemoved' => "-1",
            'ruleId' => "-1",
            'sysRule' => "",
            'applyType' => "",
            'applyStatus' => "",
            'printJh' => "-1",
            'printMd' => "-1",
            'commitPlatform' => "",
            'productStatus' => "",
            'jhComment' => "-1",
            'storageId' => "0",
            'isOversea' => "-1",
            'isFree' => "-1",
            'isBatch' => "-1",
            'history' => "",
            'custom' => "-1",
            'timeOut' => "0",
            'refundStatus' => "0",
            'buyerAccount' => "",
            'forbiddenStatus' => "-1",
            'forbiddenReason' => "0",
            'behindTrack' => "-1",
            'orderId' => "",
        ];
        $url = self::getOrderUrl($params, 'https://www.dianxiaomi.com/package/list.htm?');
        $res = of_base_com_net::request($url, [
            'cookie' => $cookie,
        ]);
        if($res['state']){
            $html = $res['response'];
            $xPaths = array(
                //包裹号
                'parcelNum' => array(
                    'xPath' => '//*[@id="orderListTable"]/tbody/tr/td[1]/a'
                ),
                //店小秘订单号
                'orderId' => [
                    'xPath' => '//*[@id="orderListTable"]/tbody/tr/td[1]/input[1]',
                    'attr' => 'value'
                ],
                //平台账号
                'pAccount' => [
                    'xPath' => '//*[@id="orderListTable"]/tbody/tr/td[4]/span',
                ],
                //订单状态
                'orderState' => [
                    'xPath' => '//*[@id="orderListTable"]/tbody/tr/td[7]',
                ],
            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $data = [];
            //整理数据
            if(empty($xPathRe['orderState'])){
                return [];
            }
            foreach ($xPathRe['orderState'] as $key => $row){
                !empty(trim($row['content'])) && $data[$key]['orderState'] = mb_substr(trim($row['content']),0, 3);
            }
            foreach ($xPathRe['parcelNum'] as $key => $row){
                !empty(trim($row['content'])) && $data[$key]['parcelNum'] = trim($row['content']);
            }
            foreach ($xPathRe['orderId'] as $key => $row){
                !empty(trim($row['content'])) && $data[$key]['orderId'] = trim($row['content']);
            }
            foreach ($xPathRe['pAccount'] as $key => $row){
                $pAccount = explode('：', trim($row['content']));
                !empty(trim($row['content'])) && $data[$key]['platform'] = mb_substr($pAccount[0], 1);
                !empty(trim($row['content'])) && $data[$key]['pAccount'] = mb_substr($pAccount[1], 0, -1);
            }
            return $data;
        }
        return false;
    }

    /**
     * 描述 : 获取退款订单列表
     * 作者 : Zoro.Zhu
     */
    public static function getRefundOrderList($cookie) {
        $params = [
            'pageNo' => 1,
            'pageSize' => "300",
            'shopId' => "-1",
            'state' => "refound",
            'platform' => "",
            'isSearch' => "1",
            'searchType' => "orderId",
            'authId' => "-1",
            'startTime' => "",
            'endTime' => "",
            'country' => "",
            'orderField' => "refunded_time",
            'isVoided' => "-1",
            'isRemoved' => "-1",
            'ruleId' => "-1",
            'sysRule' => "",
            'applyType' => "",
            'applyStatus' => "",
            'printJh' => "-1",
            'printMd' => "-1",
            'commitPlatform' => "",
            'productStatus' => "",
            'jhComment' => "-1",
            'storageId' => "0",
            'isOversea' => "-1",
            'isFree' => "-1",
            'isBatch' => "-1",
            'history' => "",
            'custom' => "-1",
            'timeOut' => "0",
            'refundStatus' => "0",
            'buyerAccount' => "",
            'forbiddenStatus' => "-1",
            'forbiddenReason' => "0",
            'behindTrack' => "-1",
            'orderId' => "",
        ];
        $url = self::getOrderUrl($params, 'https://www.dianxiaomi.com/package/list.htm?');
        $res = of_base_com_net::request($url, [
            'cookie' => $cookie,
        ]);
        if($res['state']){
            $html = $res['response'];
            $xPaths = array(
                //包裹号
                'parcelNum' => array(
                    'xPath' => '//*[@id="orderListTable"]/tbody/tr/td[1]/a'
                ),
                //店小秘订单号
                'orderId' => [
                    'xPath' => '//*[@id="orderListTable"]/tbody/tr/td[1]/input[1]',
                    'attr' => 'value'
                ],
                //退款金额
                'refund' => [
                    'xPath' => '//*[@id="orderListTable"]/tbody/tr/td[2]'
                ]
            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $data = [];
            //整理数据
            if(empty($xPathRe['orderId'])){
                return [];
            }
            foreach ($xPathRe['parcelNum'] as $key => $row){
                !empty(trim($row['content'])) && $data[$key]['parcelNum'] = trim($row['content']);
            }
            foreach ($xPathRe['orderId'] as $key => $row){
                $orderId = trim($row['content']);
                !empty(trim($row['content'])) && $data[$key]['orderId'] = $orderId;
                $xPs = array(
                    //退款金额
                    'refund' => [
                        'xPath' => '//*[@class="orderId_' . $orderId . '"]/td[2]/span'
                    ]
                );
                $refund = core_xtmDec::xPath($html, $xPs);
                $rPrice = [
                    'price' => 0,
                    'currency' => ''
                ];
                if(!empty($refund['refund'])){
                    foreach ($refund['refund'] as $re) {
                        $r = explode('-', $re['content']);
                        if(!empty($r[1])){
                            $r = explode(' ', $r[1]);
                            $rPrice['price'] += trim($r[1]);
                            $rPrice['currency'] = trim($r[0]);
                        }
                    }
                }
                $data[$key]['refund'] = $rPrice;
            }
            return $data;
        }
        return false;
    }

    /**
     * 描述 : 获取订单的URL
     * 作者 : Zoro.Zhu
     */
    public static function getOrderUrl($params, $url = null) {
        empty($url) && $url = "https://www.dianxiaomi.com/package/list.htm?";
        //页数
//        isset($params['pageNo']) && $url .= "pageNo={$params['pageNo']}&";
//        //每页多少行
//        isset($params['pageSize']) && $url .= "pageSize={$params['pageSize']}&";
//        //状态 paid待审核 approved待处理 processed运单号申请 allocated_has有货 allocated_out缺货 shipped发货成功 refound已退款 ‘’已搁置
//        isset($params['state']) && $url .= "state={$params['state']}&";
//        //排序字段  sku按产品SKU order_create_time按下单时间 order_timeout_time按剩余发货时间 order_pay_time 按付款时间 order_amount按订单金额 buyer_select_shipping_provider按买家指定物流
//        isset($params['orderField']) && $url .= "orderField={$params['orderField']}&";
//
//        //搜索类型 orderId订单号 packageNumber包裹号 trackNumber跟踪号 fCode物流商单号 sku平台SKU productSku商品SKU skuCode商品编码 buyerAccount买家账号 contactName收件人
//        isset($params['searchType']) && $url .= "searchType={$params['searchType']}&";
//        //输入框内容
//        isset($params['content']) && $url .= "content={$params['content']}&";
//        //1精确搜索 0模糊搜索
//        isset($params['orderSearchType']) && $url .= "orderSearchType={$params['orderSearchType']}&";
//        //下单时间开始
//        isset($params['orderCreateStart']) && $url .= "orderCreateStart={$params['orderCreateStart']}&";
//        //下单时间结束
//        isset($params['orderCreateEnd']) && $url .= "orderCreateEnd={$params['orderCreateEnd']}&";
       foreach ($params as $key => $val) {
           $url .= "{$key}={$val}&";
       }
        return trim($url, '&');
    }

    /**
     * 描述 : 获取订单明细
     * 作者 : Zoro.Zhu
     */
    public static function getOrderDetail($cookie, $orderId){
        $url = "https://www.dianxiaomi.com/package/detail.htm";
        $post = ['packageId' => $orderId,'history' => ''];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']){
            $html = $res['response'];
            $xPaths = array(
                //包裹号
                'parcelNum' => array(
                    'xPath' => '//*[@id="dxmPackageNumDetailSpan"]'
                ),
                //收件人
                'contact' => array(
                    'xPath' => '//*[@id="detailContact1"]'
                ),
                //电话
                'phone' => [
                    'xPath' => '//*[@id="detailPhone1"]',
                ],
                //手机
                'mobile' => [
                    'xPath' => '//*[@id="detailMobile1"]',
                ],
                //国家
                'country' => [
                    'xPath' => '//*[@id="detailCountry1"]',
                ],
                //省  / 州
                'province' => [
                    'xPath' => '//*[@id="detailProvince1"]',
                ],
                //城市
                'city' => [
                    'xPath' => '//*[@id="detailCity1"]',
                ],
                //地址1
                'address1' => [
                    'xPath' => '//*[@id="detailAddr11"]',
                ],
                //门牌号
                'apartmentNumber1' => [
                    'xPath' => '//*[@id="apartmentNumber1"]',
                ],
                //地址2
                'address2' => [
                    'xPath' => '//*[@id="detailAddress21"]',
                ],
                //邮编
                'zip' => [
                    'xPath' => '//*[@id="detailZip1"]',
                ],
                //公司
                'companyName' => [
                    'xPath' => '//*[@id="companyName1"]',
                ],
                //税号
                'taxNumber' => [
                    'xPath' => '//*[@id="taxNumber1"]',
                ],
                //平台订单号
                'pOrderId' => [
                    'xPath' => '//tr[@class="orderInfoCon pairProInfoBox"]/td[1]//a',
                ],
                //报关信息
                'product' => [
                    'xPath' => '//td[@class="garyText nomalLineHeight"]',
                ],
                //产品图片
                'img' => [
                    'xPath' => '//img[@class="imgCss pairProInfoImg"]',
                    'attr' => 'src',
                ],
                //SKU和数量
                'skuAndQty' => [
                    'xPath' => '//span[@class="pairProInfoSku"]',
                ],
                //订单金额
                'skuPrice' => [
                    'xPath' => '//table[@class="tableIn pairProInfo"]//tr[1]/td[2]//p[2]',
                ],
                //买家指定物流
                'buyerLogistics' => [
                    'xPath' => '//*[@id="logisticsInformation"]/div[2]/form/div/div[1]/div[1]',
                ],
                //物流信息获取方式
                'trackNumLabel' => [
                    'xPath' => '//*[@id="trackNumLabel"]',
                ],
                //物流方式
                'logistics' => [
                    'xPath' => '//*[@id="agentProviderDiv"]/a/span',
                ],
                //物流方式
                'trackNo' => [
                    'xPath' => '//*[@id="trackNumberDiv"]',
                ],
                //拣货备注
                'pickingRemark' => [
                    'xPath' => '//*[@id="jhCommentTrDetail"]/td[1]',
                ],

            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $data['parcelNum'] = isset($xPathRe['parcelNum'][0]['content']) ? trim($xPathRe['parcelNum'][0]['content']) : '';
            $data['contact'] = isset($xPathRe['contact'][0]['content']) ? trim($xPathRe['contact'][0]['content']) : '';
            $data['phone'] = isset($xPathRe['phone'][0]['content']) ? trim($xPathRe['phone'][0]['content']) : '';
            $data['mobile'] = isset($xPathRe['mobile'][0]['content']) ? trim($xPathRe['mobile'][0]['content']) : '';
            $data['countryEn'] = isset($xPathRe['country'][0]['content']) ? substr($xPathRe['country'][0]['content'], 0 ,strpos($xPathRe['country'][0]['content'], '（')) : '';
            $data['country'] = isset($xPathRe['country'][0]['content']) ? substr($xPathRe['country'][0]['content'], strpos($xPathRe['country'][0]['content'], '（') + 3 ,strpos($xPathRe['country'][0]['content'], '）') - strpos($xPathRe['country'][0]['content'], '（') - 3) : '';
            $data['province'] = isset($xPathRe['province'][0]['content']) ? trim($xPathRe['province'][0]['content']) : '';
            $data['city'] = isset($xPathRe['city'][0]['content']) ? trim($xPathRe['city'][0]['content']) : '';
            $data['address1'] = isset($xPathRe['address1'][0]['content']) ? trim($xPathRe['address1'][0]['content']) : '';
            $data['apartmentNumber1'] = isset($xPathRe['apartmentNumber1'][0]['content']) ? trim($xPathRe['apartmentNumber1'][0]['content']) : '';
            $data['address2'] = isset($xPathRe['address2'][0]['content']) ? trim($xPathRe['address2'][0]['content']) : '';
            $data['zip'] = isset($xPathRe['zip'][0]['content']) ? trim($xPathRe['zip'][0]['content']) : '';
            $data['companyName'] = isset($xPathRe['companyName'][0]['content']) ? trim($xPathRe['companyName'][0]['content']) : '';
            $data['taxNumber'] = isset($xPathRe['taxNumber'][0]['content']) ? trim($xPathRe['taxNumber'][0]['content']) : '';
            $data['buyerLogistics'] = isset($xPathRe['buyerLogistics'][0]['content']) ? trim($xPathRe['buyerLogistics'][0]['content']) : '';
            $data['trackNumLabel'] = isset($xPathRe['trackNumLabel'][0]['content']) ? trim($xPathRe['trackNumLabel'][0]['content']) : '';
            $data['logistics'] = isset($xPathRe['logistics'][0]['content']) ? trim($xPathRe['logistics'][0]['content']) : '';
            $data['trackNo'] = isset($xPathRe['trackNo'][0]['content']) ? trim($xPathRe['trackNo'][0]['content']) : '';
            $data['pickingRemark'] = isset($xPathRe['pickingRemark'][0]['content']) ? trim($xPathRe['pickingRemark'][0]['content']) : '';
            $productInfo = [];
            foreach ($xPathRe['pOrderId'] as $key => $row){
                $orderId = trim($row['content']);
                $skuInfo = preg_replace( '/[^\S]/', '', $xPathRe['skuAndQty'][$key]['content']);
                $skuPrice = preg_replace( '/[^\S]/', '', $xPathRe['skuPrice'][$key]['content']);
                $product = explode('/', $xPathRe['product'][$key]['content']);
                $productInfo[$orderId] = [
                    'sku' => substr($skuInfo, 0, strripos($skuInfo, 'x') - 2),
                    'qty' => substr($skuInfo, strripos($skuInfo, 'x') + 3),
                    'currency' => substr($skuPrice, 0, 3),
                    'price' => substr($skuPrice, 3),
                    'img' => str_replace("-tiny.jpg","-normal.jpg",$xPathRe['img'][$key]['content']),
                    'pNameCn' => !empty($product[0]) ? mb_substr(trim($product[0]), 0, -2) : '',
                    'pNameEn' => !empty($product[1]) ? mb_substr(trim($product[1]), 2, -2) : '',
                ];
            }
            $data['productInfo'] = $productInfo;
            return $data;
        }
    }

    /**
     * 描述 : 获取包裹总金额和下单时间
     * 作者 : Zoro.Zhu
     */
    public static function getOrderPrice($cookie, $parcelNum, $orderId){
        $params = [
            'searchTypes' => "packageNum",
            'contents' => $parcelNum,
            'state' => "",
            'isVoided' => "-1",
            'isRemoved' => "-1",
            'commitPlatforms' => "",
            'isOversea' => "-1",
            'shopId' => "-1",
            'platform' => "",
            'history' => "",
            'orderField' => "order_create_time",
            'isDesc' => "1",
            'timeOut' => "0",
            'isGreen' => "0",
            'isYellow' => "0",
            'isOrange' => "0",
            'isRed' => "0",
            'isViolet' => "0",
            'isBlue' => "0",
            'cornflowerBlue' => "0",
            'pink' => "0",
            'teal' => "0",
            'turquoise' => "0",
            'forbiddenStatus' => "-1",
            'forbiddenReason' => "0",
            'priceStart' => "",
            'priceEnd' => "",
            'orderCreateStart' => "",
            'orderCreateEnd' => "",
            'orderPayStart' => "",
            'orderPayEnd' => "",
            'shippedStart' => "",
            'shippedEnd' => "",
            'timeOutQuery' => "-1",
            'productCountStart' => "",
            'productCountEnd' => "",
            'storageId' => "0",
            'authId' => "-1",
            'country' => "",
            'isPrintJh' => "-1",
            'signPriorShip' => "-1",
            'isPrintMd' => "-1",
            'commitPlatform' => "",
            'isHasOrderMessage' => "-1",
            'isHasOrderComment' => "-1",
            'isHasServiceComment' => "-1",
            'globalCollection' => "-1",
        ];
        $url = 'https://www.dianxiaomi.com/package/advancedSearch.htm';
        $res = of_base_com_net::request($url, [
            'cookie' => $cookie,
            'post' => $params,
            'header' => [
                'Host: www.dianxiaomi.com',
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
            ],
        ]);
        if($res['state']){
            $html = $res['response'];
//            echo $html;exit;
            $xPaths = array(
                //订单金额
                'priceTotal' => array(
                    'xPath' => '//*[@id="orderListTable"]/tbody/tr/td[2]'
                ),
                //平台订单号
                'orderId' => [
                    'xPath' => '//td[@class="tableOrderId"]/a',
                ],
                //各项时间
                'time' => [
                    'xPath' => '//*[@id="orderListTable"]/tbody/tr/td[5]',
                ],
                //打印面单时间
                'printTime' => [
                    'xPath' => "//*[@id=\"printMd{$orderId}\"]",
                    'attr' => 'title',
                ],

            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            //整理数据
            if(empty($xPathRe['orderId'])){
                return [];
            }
            $data = [];
            foreach ($xPathRe['priceTotal'] as $key => $row){
                if($key === 0 || empty(trim($row['content']))){
                    continue;
                }
                $row['content'] = preg_replace( '/[^A-Z0-9.]/', '', $row['content']);
                $data[$key - 1]['priceTotal'] = substr(trim($row['content']), 3);
                $data[$key - 1]['currency'] = substr(trim($row['content']), 0,3);
            }
            foreach ($xPathRe['orderId'] as $key => $row){
                !empty(trim($row['content'])) && $data[$key]['orderId'] = trim($row['content']);
            }
            foreach ($xPathRe['time'] as $key => $row){
                !empty(trim($row['content'])) && $data[$key]['createTime'] = substr($row['content'],strpos($row['content'],'下单') + 9,16) . ':00';
                !empty(trim($row['content'])) && $data[$key]['paymentTime'] = substr($row['content'],strpos($row['content'],'付款') + 9,16) . ':00';
                !empty(trim($row['content'])) && $data[$key]['submitTime'] = substr($row['content'],strpos($row['content'],'提交') + 9,16) . ':00';
                !empty(trim($row['content'])) && $data[$key]['shipTime'] = substr($row['content'],strpos($row['content'],'发货') + 9,16) . ':00';
            }
            foreach ($xPathRe['printTime'] as $key => $row){
                !empty(trim($row['content'])) && $data[$key]['printTime'] = mb_substr($row['content'], 10) . ':00';
            }
            $newData = [];
            foreach($data as $row){
                $newData[$row['orderId']] = $row;
            }
            return $newData;
        }
        return false;
    }

    /**
     * 描述 : 获取物流节点信息
     * 作者 : Zoro.Zhu
     */
    public static function getTrackDog($trackNo){
//        $ipUrl = "http://intelligence.yafex.cn/api/?c=proxy&a=getProxy";
//        $ipResult = of_base_com_net::request($ipUrl, array('get' => array(), 'timeout' => 30));
//        if (!empty($ipResult['response'])) {
//            $ipDate = json_decode($ipResult['response'], true);
//            $ipDate = $ipDate['data'];
//        }
        $url = "https://ext.trackdog.com/ajaxTrack.json";
        $post = json_encode([
            [
                'trackNum' => $trackNo,
                'code' => 'wishpost',
                'userTag' => '1509318',
                'pf' => 'wish',
                'pf_c' => 'WishPost',
            ]
        ]);
        $re = [
            'post' => $post,
            'header' => [
                'Content-Type:application/json;charset=UTF-8',
            ],
        ];
//        !empty($ipDate['proxy']) && $re['header'][] = "X-FORWARDED-FOR:{$ipDate['proxy']}";
        $res = of_base_com_net::request($url,$re);
//        core_base::log($res, 'trackDogLog');
        if($res['state']){
            $response = json_decode($res['response'], true);
            if($response['code'] == 0){
                //第一次拿到的数据好像有问题
                if(empty($response['data'][0])){
                    return null;
                }else{
                    return $response['data'][0];
                }
            }
        }
    }

    /**
     * 描述 : 获取渠道列表
     * 作者 : Zoro.Zhu
     */
    public static function getLogisticsList($cookie){
        $url = "https://www.dianxiaomi.com/package/commSearchList.htm";
        $post = [
            'prefixCount' => 200,
            'history' => '',
            'state' => '',
            'isVoided' => -1,
            'isOversea' => -1,
            'commitPlatform' => '',
            'newVersion' => 0,
            'isBatch' => -1,
            'isFree' => -1,
        ];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']){
            $html = $res['response'];
            $xPaths = array(
                //渠道列表
                'list' => array(
                    'xPath' => '//*[@id="agentIdAndDetil"]',
                    'attr' => 'value',
                ),

            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $list = $xPathRe['list'][0]['content'];
            return $list;
        }
        return false;
    }

    /**
     * 描述 : 标记打印完成
     * 作者 : Zoro.Zhu
     */
    public static function printOk($cookie,$orderId,$logisticsId){
        $url = "https://www.dianxiaomi.com/package/batchSetPrint.json";
        $post = ['packageIds' => $orderId,'authId' => $logisticsId,'type' => 1,'isPrint' => 1];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']){
            $data = json_decode($res['response'], true);
            if(!empty($data['bor']['successList'][0]['id']) && $data['bor']['successList'][0]['id'] == $orderId){
                return true;
            }
        }
        return false;
    }

    /**
     * 描述 : 获取面单信息
     * 作者 : Zoro.Zhu
     */
    public static function getLabelInfo($cookie, $orderId, $logisticsId){
        $url = "https://www.dianxiaomi.com/printMb.htm?packageIds={$orderId}&authId={$logisticsId}&isCos=1";
        $res = of_base_com_net::request($url, ['cookie' => $cookie,]);
        $local = substr($res['header'],strpos($res['header'], 'Location:') + 10);
        $pdf = of_base_com_net::request(trim($local));
        $path = core_base::pdf($pdf['response'], $orderId);
        return ['url' => $path, 'pdf' => base64_encode($pdf['response'])];
    }

    /**
     * 描述 : 同步PB广告
     * 作者 : Zoro.Zhu
     */
    public static function getPb($cookie){
        $url = "https://www.dianxiaomi.com/wishProductBoost/syncListCampaigns.json";
        $post = ['shopId' => 0];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']){
            $data = json_decode($res['response'], true);
            if(!empty($data['uuid'])){
                return $data['uuid'];
            }
        }
        return false;
    }

    /**
     * 描述 : 查询PB广告是否同步结束
     * 作者 : Zoro.Zhu
     */
    public static function getPbEnd($cookie, $uuid){
        $url = "https://www.dianxiaomi.com/checkProcess.json";
        $post = ['uuid' => $uuid];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']){
            $data = json_decode($res['response'], true);
            if($data['processMsg']['code'] === 0){
                sleep(10);
                self::getPbEnd($cookie, $uuid);
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 描述 : 获取PB广告进行中的列表
     * 作者 : Zoro.Zhu
     */
    public static function getPbList($cookie){
        $url = "https://www.dianxiaomi.com/wishProductBoost/pageList.htm";
        $post = [
            "pageNo" => 1,
            "pageSize" => 100,
            "shopId" => 0,
            "state" => "STARTED", //STARTED 进行中 SAVED 预设中
            "searchType" => 0,
            "searchValue" => "",
            "sortName" => 9,
            "sortValue" => 0,
            "budgetLeft" => "",
            "budgetRight" => "",
            "spendLeft" => "",
            "spendRight" => "",
            "transRatioLeft" => "",
            "transRatioRight" => "",
            "salesLeft" => "",
            "salesRight" => "",
            "startTimeLeft" => "",
            "startTimeRight" => "",
            "impressionsLeft" => "",
            "impressionsRight" => "",
            "gmvRight" => "",
            "gmvLeft" => "",
            "budgetRatioLeft" => "",
            "budgetRatioRight" => "",
            "hasLowBudget" => 0,
        ];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']) {
            $html = $res['response'];
            $xPaths = array(//订单金额
                'pbId' => ['xPath' => '/html/body/table/tbody/tr/td[2]/span'], //活动编号
                'pbState' => ['xPath' => '/html/body/table/tbody/tr/td[12]'], //广告状态
            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $pdList = [];
            if(!empty($xPathRe['pbId'])){
                foreach($xPathRe['pbId'] as $key => $row){
                    $pbState = preg_replace( '/[^\S]/', '', $xPathRe['pbState'][$key]['content']);
                    $pbState = explode("(", $pbState);
                    $pdList[$row['content']] = $pbState[0];
                }
            }
            return $pdList;
        }
        return false;
    }

    /**
     * 描述 : 修改pb广告状态
     * 作者 : Zoro.Zhu
     */
    public static function editPb($cookie,$pbid){
        $url = "https://www.dianxiaomi.com/wishProductBoost/batchEdit.json";
        $post = ['campaignIds' => $pbid, 'action' => 'stop'];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']){
            $data = json_decode($res['response'], true);
            if(!empty($data['uuid'])){
                return $data['uuid'];
            }
        }
        return false;
    }

    /**
     * 描述 : 同步wish产品信息
     * 作者 : Zoro.Zhu
     */
    public static function syncProduct($cookie){
        $url = "https://www.dianxiaomi.com/product/syncProductFromWish.json";
        $post = ['shopId' => -1];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']){
            $data = json_decode($res['response'], true);
            if(!empty($data['uuid'])){
                return $data['uuid'];
            }
        }
        return false;
    }

    /**
     * 描述 : 同步订单信息
     * 作者 : Zoro.Zhu
     */
    public static function syncOrder($cookie){
        $url = "https://www.dianxiaomi.com/order/syncOrder.json";
        $post = ['shopId' => -1, 'platform' => ''];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']){
            $data = json_decode($res['response'], true);
            if(!empty($data['uuid'])){
                return $data['uuid'];
            }
        }
        return false;
    }

    /**
     * 描述 : 重新交运
     * 作者 : Zoro.Zhu
     */
    public static function getTrack($cookie){
        $url = "https://www.dianxiaomi.com/order/batchReApply.json";
        $post = ['type' => 0, 'packageIds' => ''];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']){
            $data = json_decode($res['response'], true);
            if(!empty($data['uuid'])){
                return $data;
            }
        }
        return false;
    }

    /**
     * 描述 : 采集数据
     * 作者 : Zoro.Zhu
     */
    public static function getItemInfo($cookie, $url){
        $u = "https://www.dianxiaomi.com/crawl/crawl.json";
        $post = ['urls' => $url, 'repeatCheck' => 1];
        $res = of_base_com_net::request($u, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ], true);
        if($res['state']){
            return true;
        }
        return false;
    }

    /**
     * 描述 : 获取产品信息
     * 作者 : Zoro.Zhu
     */
    public static function getProduct($cookie, $params = []){
        $url = "https://www.dianxiaomi.com/product/pageList.htm";
        $post = [
            'pageNo' => 1,
            'pageSize' => $params['pageSize'],
            'shopId' => $params['shopId'],
            'fullCid' => '',
            'state' => $params['state'],
            'productState' => $params['productState'],
            'searchType' => 0,
            'searchValue' => '',
            'sortName' => 4,
            'sortValue' => 1,
            'reviewState' => 'approved,pending',
            'isPromoted' => '',
            'isWishexpress' => '',
            'hasCleanImg' => '',
            'productSearchType' => 1,
            'advancedSearch' => 'yes',
            'productMonitor' => -1,
            'priceLift' => '',
            'priceRight' => '',
            'localizedPriceLift' => '',
            'localizedPriceRight' => '',
            'totalPriceLift' => '',
            'totalPriceRight' => '',
            'inventoryLift' => $params['inventoryLift'],
            'inventoryRight' => $params['inventoryRight'],
            'saveLift' => '',
            'saveRight' => '',
            'soldLift' => '',
            'soldRight' => '',
            'timeLift' => '',
            'timeRight' => '',
            'shippingRight' => '',
            'shippingLeft' => '',
            'localizedShippingRight' => '',
            'localizedShippingLeft' => '',
            'comment' => 2,
            'advancedTime' => 1,
            'hasMSRP' => '',
            'hasVideo' => '',
        ];
        $res = of_base_com_net::request($url, [
            'post' => $post,
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']) {
            $html = $res['response'];
            $xPaths = array(//订单金额
                'productId' => ['xPath' => '//*[@id="sysProMsg"]/tr/td[1]/label/input/@value'], //产品Id
            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $productList = [];
            if(!empty($xPathRe['productId'])){
                foreach($xPathRe['productId'] as $key => $row){
                    $productList[$row['content']] = $row['content'];
                }
            }
            return $productList;
        }
        return false;
    }

    /**
     * 描述 : 获取待审核状态的订单
     * 作者 : Zoro.Zhu
     */
    public static function getPendingOrder($cookie){
        $url = "https://www.dianxiaomi.com/package/list.htm?pageNo=1&pageSize=300&shopId=-1&state=paid&authId=-1&country=&platform=&isSearch=0&startTime=&endTime=&orderField=order_pay_time&isVoided=0&isRemoved=0&ruleId=-1&sysRule=&applyType=&applyStatus=&printJh=-1&printMd=-1&commitPlatform=&productStatus=&jhComment=-1&storageId=0&history=&custom=-1&isOversea=-1&timeOut=0&refundStatus=0&forbiddenStatus=-1&forbiddenReason=0&behindTrack=-1";
        $res = of_base_com_net::request($url, [
            'post' => [],
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']) {
            $html = $res['response'];
            $xPaths = array(//订单金额
                'orderId' => ['xPath' => '//*[@id="orderListTable"]/tbody/tr/td[1]/input[1]/@value'], //订单ID
            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $orderList = [];
            if(!empty($xPathRe['orderId'])){
                foreach($xPathRe['orderId'] as $key => $row){
                    $orderList[$row['content']] = $row['content'];
                }
            }
            return $orderList;
        }
        return false;
    }

    /**
     * 描述 : 获取账号信息
     * 作者 : Zoro.Zhu
     */
    public static function getAccountId($cookie){
        $url = "https://www.dianxiaomi.com/product/offline.htm?productState=0&state=0";
        $res = of_base_com_net::request($url, [
            'get' => [],
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ]);
        if($res['state']) {
            $html = $res['response'];
            $xPaths = array(//订单金额
                'accountId' => ['xPath' => '//*[@id="myTabContentOne"]/div[1]/div[2]/table/tbody/tr[1]/td[2]/a/@data-value'], //账号ID
            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $orderList = [];
            if(!empty($xPathRe['accountId'])){
                foreach($xPathRe['accountId'] as $key => $row){
                    !empty($row['content']) && $orderList[$row['content']] = $row['content'];
                }
            }
            return $orderList;
        }
    }


    /**
     * 描述 : 获取账号信息
     * 作者 : Zoro.Zhu
     */
    public static function publishProduct($cookie,$productId){
//        $url = "https://www.dianxiaomi.com/product/checkProduct.json?id=" . $productId;
        $url2 = "https://www.dianxiaomi.com/product/publish.json?id=" . $productId;
//        $res = of_base_com_net::request($url, [
//            'get' => [],
//            'cookie' => $cookie,
//            'header' => [
//                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
//                'Host: www.dianxiaomi.com',
//                'Origin: https://www.dianxiaomi.com',
//                'Referer: https://www.dianxiaomi.com/order/index.htm',
//                'Sec-Fetch-Mode: cors',
//            ]
//        ]);
//        p($res);
        $res = of_base_com_net::request($url2, [
            'get' => [],
            'cookie' => $cookie,
            'header' => [
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
                'Host: www.dianxiaomi.com',
                'Origin: https://www.dianxiaomi.com',
                'Referer: https://www.dianxiaomi.com/order/index.htm',
                'Sec-Fetch-Mode: cors',
            ]
        ], true);
    }
}
return true;